package com.controller.sys;

import com.common.Result;
import com.config.security.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;

@Api("TokenController")
@RestController
@RequestMapping("/token")
public class TokenController {

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Resource
    private UserDetailsService userDetailsService;

    @ApiOperation("验证token")
    @GetMapping("/validatorToken/{token}")
    public Result validatorToken(@PathVariable String token, Principal principal) {
        String username = principal.getName();
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        return Result.success(jwtTokenUtil.validateToken(token, userDetails));
    }
}
